package br.com.vm.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import br.com.vm.model.base.Base;

@Entity
@Table(name = "MAPA")
public class Mapa extends Base {

	private static final long serialVersionUID = 2505374365006657435L;
	@Id
	@Column(name = "ID")
	@GeneratedValue
	private Integer id;
	@Column(name = "NOME", unique = true, nullable = false)
	private String nome;

	@SuppressWarnings("unchecked")
	@OneToMany(mappedBy = "mapa", fetch = FetchType.EAGER)
	@Cascade({ CascadeType.SAVE_UPDATE, CascadeType.PERSIST, CascadeType.MERGE,
			CascadeType.ALL })
	private List<Rota> rotas = LazyList.decorate(new ArrayList<Rota>(),
			FactoryUtils.instantiateFactory(Rota.class));

	public Mapa() {
	}

	public Mapa(final Integer piIdMapa) {
		this.id = piIdMapa;
	}

	@Override
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public List<Rota> getRotas() {
		return rotas;
	}

	public void setRotas(List<Rota> Rotas) {
		this.rotas = Rotas;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((rotas == null) ? 0 : rotas.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Mapa other = (Mapa) obj;
		if (rotas == null) {
			if (other.rotas != null)
				return false;
		} else if (!rotas.equals(other.rotas))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}
}
